R package to use Google’s gemma (and gemini) via API on R.
This package was modified from gemini.R [https://jhk0530.github.io/gemini.R/]. I thank the original author for the wonderful package.
This is a customized fork, combining both gemma and gemini in generic functions. The original author of gemini.R has also added separate gemma_* functions for using gemma. So, this fork attempts to keep the functions simple, providing functions that work for both gemma and gemini.
Take note that many parts of the original package are still not modified. These are the parts (from which the functions come from) that work:
gemini.Rgemini_chat.Rgemini_image.R# install.packages("pak")
pak::pak("wnarifin/rgemma")
library(gemini.R)
setAPI("your API key") # check https://makersuite.google.com/app/apikey
gemini("Explain about the gemini in astrology in one line")
# text
# "Gemini, the third astrological sign, is associated with
# communication, adaptability, and a thirst for knowledge."
# uses default prompt as "Explain this image"
# uses included image of gemini.R package
gemini_image(image = system.file("docs/reference/figures/image.png", package = "gemini.R"))
# text
# " The image shows a table with a white tablecloth. On the table are two cups of coffee,
# a bowl of blueberries, and five scones. The scones are covered in blueberries and have
# a crumbly texture. There are also some pink flowers on the table. The background is a
# dark blue color. The image is taken from a top-down perspective."
# use default prompt as "Describe this audio"
# uses included audio of gemini.R package
gemini_audio(audio = system.file("docs/reference/helloworld.mp3", package = "gemini.R"))
# text
# "The audio contains a clear, crisp recording of a person saying \"Hello world\".
# Following the phrase is a distinct, repeated percussive sound, possibly a rhythmic
# tapping or clicking noise. The sound is consistent and mechanical in nature.\n"
To use gemini_image function, image file required. and I
recommend to use shiny app with fileInput function.
library(shiny)
library(gemini.R)
setAPI("YOUR KEY")
ui <- fluidPage(
sidebarLayout(
NULL,
mainPanel(
fileInput(
inputId = "file",
label = "Choose file to upload",
),
imageOutput(outputId = "image1"),
textInput(
inputId = "prompt",
label = "Prompt",
placeholder = "Enter Prompts Here"
),
actionButton("goButton", "Ask to gemini"),
textOutput("text1")
)
)
)
server <- function(input, output) {
observeEvent(input$file, {
path <- input$file$datapath
output$image1 <- renderImage({
list(
src = path
)
}, deleteFile = FALSE)
})
observeEvent(input$goButton, {
output$text1 <- renderText({
gemini_image(input$prompt, input$file$datapath)
})
})
}
shinyApp(ui = ui, server = server)
gemini_docs(
c("doc1.pdf", "doc2.pdf"),
prompt = "Summarize each document separately."
)
tokens <- token.vertex("YOUR_API_KEY.json", model_id = "1.5-flash")
prompt <- "What is sachins Jersy number?"
gemini.vertex(prompt, tokens)
tokens <- token.vertex("YOUR_API_KEY.json", model_id = "1.5-flash")
gemini_image.vertex(image = "YOUR_IMAGE.png", type, tokens)
tokens <- token.vertex("YOUR_API_KEY.json", model_id = "1.5-flash")
gemini_audio.vertex(audio = "YOUR_AUDIO.mp3", tokens)
gen_image('Draw an image of a cat swimming in the Milky Way')
gemini_search('What is the current Apple stock price?')
gemini_searchR("Who won the latest F1 grand prix?")
You may customize keyboard shortcut for this feature.
Before use the API, I recommend that you to check at least the following.
There may be more terms and conditions that you need to check. Any app which uses the API should be compliant with the Google Terms of Service.
[!note]
image is from Google AI for Developers
I’ve got many inspiration from Deepanshu Bhalla’s article